using System;
using System.Collections.Generic;
using Holoville.HOTween;
using UnityEngine;

public class ParticleAnimation : MonoBehaviour
{
	private void OnEnable()
	{
		this.StartAnimation();
	}

	private void OnDisable()
	{
		this.StopAnimation();
	}

	public void StartAnimation()
	{
		Vector3 a = this.cam.GetComponent<Camera>().ScreenToWorldPoint(Vector3.zero);
		Vector3 vector = a - this.cam.gameObject.transform.position;
		if (this.isRisingAnimation)
		{
			this.startY = -Mathf.Abs(vector.y) - 1.5f;
		}
		else
		{
			this.startY = -Mathf.Abs(vector.y);
		}
		this.endY = Mathf.Abs(this.startY) + 1f;
		this.leftX = -Mathf.Abs(vector.x);
		this.rightX = Mathf.Abs(this.leftX);
		for (int i = 0; i < 10; i++)
		{
			this.CreateParticle();
		}
	}

	public void StopAnimation()
	{
		foreach (GameObject gameObject in this.particleList)
		{
			this.StopTween(gameObject);
			UnityEngine.Object.Destroy(gameObject);
		}
		this.particleList.Clear();
	}

	private void CheckParticle()
	{
		foreach (GameObject gameObject in this.particleList)
		{
			if (gameObject.transform.localPosition.y >= this.endY || gameObject.transform.localPosition.y <= this.startY)
			{
				this.StartTween(gameObject);
				break;
			}
		}
	}

	private void CreateParticle()
	{
		GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.prefabParticle);
		this.particleList.Add(gameObject);
		gameObject.transform.parent = base.transform;
		gameObject.transform.localPosition = new Vector3(0f, this.startY, 3f);
		this.StartTween(gameObject);
	}

	private void StartTween(GameObject particle)
	{
		float num = UnityEngine.Random.Range(this.leftX, this.rightX);
		float y = (!this.isRisingAnimation) ? this.endY : this.startY;
		float y2 = (!this.isRisingAnimation) ? this.startY : this.endY;
		particle.transform.localPosition = new Vector3(num, y, 3f);
		particle.transform.eulerAngles = new Vector3(0f, 0f, (float)UnityEngine.Random.Range(0, 360));
		particle.GetComponent<tk2dSprite>().spriteName = string.Format("{0}_{1}", this.particleSpriteName, UnityEngine.Random.Range(1, 5));
		float num2;
		float p_duration;
		if (this.isRisingAnimation)
		{
			num2 = UnityEngine.Random.Range(0.3f, 0.7f);
			particle.transform.localScale = Vector3.one * (num2 + 0.3f);
			particle.GetComponent<tk2dSprite>().color = new Color(1f, 1f, 1f, num2);
			p_duration = 30f - num2 * 30f;
		}
		else
		{
			num2 = UnityEngine.Random.Range(0.1f, 0.3f);
			particle.transform.localScale = Vector3.one * (num2 + 0.7f);
			particle.GetComponent<tk2dSprite>().color = new Color(1f, 1f, 1f, num2 + 0.4f);
			p_duration = 40f - num2 * 40f;
		}
		HOTween.To(particle.transform, p_duration, new TweenParms().Prop("localPosition", new Vector3(particle.transform.localPosition.x, y2, particle.transform.localPosition.z), false).Ease(EaseType.Linear).Loops(-1).Id(particle.GetInstanceID() + "move").Delay(UnityEngine.Random.Range(0f, 2f)).OnStepComplete(delegate(TweenEvent data)
		{
			if (data.parms[0] != null)
			{
				((GameObject)data.parms[0]).GetComponent<tk2dSprite>().spriteName = string.Format("{0}_{1}", this.particleSpriteName, UnityEngine.Random.Range(1, 5));
			}
		}, new object[]
		{
			particle
		}).OnUpdate(delegate(TweenEvent Data)
		{
			if (!this.isRisingAnimation)
			{
				if (Data.parms[0] != null)
				{
					Transform transform = ((GameObject)Data.parms[0]).transform;
					if (transform.localPosition.x >= (float)Data.parms[1] + 3f)
					{
						transform.Translate(-0.5f, 0f, 0f);
					}
					else if (transform.localPosition.x < (float)Data.parms[1] + 3f)
					{
						transform.Translate(0.5f, 0f, 0f);
					}
				}
			}
		}, new object[]
		{
			particle,
			num
		}));
		float num3 = (float)((UnityEngine.Random.Range(0, 2) != 0) ? -90 : 90) * 1f / num2;
		float p_duration2;
		if (this.isRisingAnimation)
		{
			p_duration2 = 30f - num2 * 30f;
		}
		else
		{
			p_duration2 = 15f - num2 * 15f;
		}
		HOTween.To(particle.transform, p_duration2, new TweenParms().Prop("eulerAngles", new Vector3(0f, 0f, particle.transform.eulerAngles.z + num3), false).Ease(EaseType.Linear).Loops(-1, LoopType.Yoyo).Id(particle.GetInstanceID() + "rotate"));
	}

	private void StopTween(GameObject particle)
	{
		HOTween.Kill(particle.GetInstanceID() + "move");
		HOTween.Kill(particle.GetInstanceID() + "rotate");
	}

	private List<GameObject> particleList = new List<GameObject>();

	public Camera cam;

	private float startY = -10f;

	private float endY = 10f;

	private float leftX = -10f;

	private float rightX = 10f;

	[SerializeField]
	private GameObject prefabParticle;

	[SerializeField]
	private string particleSpriteName;

	[SerializeField]
	private bool isRisingAnimation = true;
}
